package net.huashitong.taskjob.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.taskjob.dao.JobGroupDao;
import net.huashitong.taskjob.entity.JobGroup;
import net.huashitong.taskjob.filter.JobGroupFilter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JobGroupService extends StringPKService<JobGroup> {
    @Autowired
    private JobGroupDao dao;
    @Autowired
    private SysTableService sysTableService;

    @Override
    protected BaseDao<JobGroup, String> getDao() {
        return dao;
    }


    @Override
    protected void beforeSave(JobGroup obj) {
        obj.setSysId(sysTableService.updateMaxSysId("t_jobgroup", null));
    }


    //保存督查小组
    public void saveGroup(JobGroupFilter filter) {
        JobGroup jg = new JobGroup();
        BeanUtils.copyProperties(filter, jg);
        save(jg);

    }



    public List<JobGroup> queryByFilter(JobGroupFilter filter) {
        List<JobGroup> list = dao.queryByFilter(filter);
        return list;
    }

    /**
     * 处理多个督查组
     * @param code
     * @return
     */
    public JobGroup getbyCode(String code)
    {
        JobGroup jobGroup  = new JobGroup();
        String tel="";
        String user="";
        String names ="";
        if(StringUtils.isNotBlank(code))
        {
            String[] arrCode =code.split(",");
            for(String item: arrCode)
            {
                JobGroup jobGroup1= dao.getJobInfobyCode(item.trim());
                tel=jobGroup1.getGroupTel()+","+tel;
                user=jobGroup1.getGroupContact()+","+user;
                names=jobGroup1.getGroupName()+","+names;
            }

        }
        jobGroup.setGroupTel(tel);
        jobGroup.setGroupContact(user);
        jobGroup.setGroupName(names);
        return jobGroup;
    }

    /**
     * 处理多个督查组
     * @param name
     * @return
     */
    public JobGroup getbyChart(String name)
    {
        JobGroup jobGroup  = new JobGroup();
        String tel="";
        String user="";
        if(StringUtils.isNotBlank(name))
        {
            String[] arrCode =name.split(",");
            for(String item: arrCode)
            {
                JobGroup jobGroup1= dao.getJobInfobyName(item.trim());
                tel=jobGroup1.getGroupTel()+","+tel;
                user=jobGroup1.getGroupContact()+","+user;
            }

        }
        jobGroup.setGroupTel(tel);
        jobGroup.setGroupContact(user);
        return jobGroup;
    }

}